﻿using MediatR;
using ProductionManagement.Application.Command.ProductionPlanCommand;
using ProductionManagement.Domain.Dtos.ProductionPlanDtos;
using ProductionManagement.Domain.ProductionDomain;
using ProductionManagement.Dtos;
using ProductionManagement.Infrstructre.lnterface;
using SqlSugar;
using System.Numerics;

namespace ProductionManagement.Application.CommandHandler.ProductionPlanCommandHandler
{
    public class ProductionPlanSelectCommandHandler : IRequestHandler<ProductionPlanSelectCommand, PageDTO<SelectProductionPlanDto>>
    {
        private readonly IProductionPlanRepository _productionPlanRep;
        private readonly IProductionProductRepository _productionProductRep;
        private readonly ISqlSugarClient _db;

        public ProductionPlanSelectCommandHandler(IProductionPlanRepository productionPlanRep, IProductionProductRepository productionProductRep, ISqlSugarClient db)
        {
            this._productionPlanRep = productionPlanRep;
            this._productionProductRep = productionProductRep;
            this._db = db;
        }

        public async Task<PageDTO<SelectProductionPlanDto>> Handle(ProductionPlanSelectCommand request, CancellationToken cancellationToken)
        {
            var list = _db.Queryable<ProductionPlan, ProductionProduct>((plan, prouduct) => new JoinQueryInfos(JoinType.Inner, plan.PPDocumentNumber == prouduct.PPDocumentNumber))
                .Where((plan, prouduct) => plan.IsDel == false)
                 .GroupBy(plan => new { plan.ProductionPlansId, plan.PreProductionPlanId, plan.PreProductionState, plan.PPDocumentNumber, plan.PPlanName, plan.PPTotalQuantity, plan.Whetherautomatic, plan.SaleContractId, plan.PPlanSmark })
                .Select((plan, prouduct) => new SelectProductionPlanDto
                {
                    ProductionPlansId = plan.ProductionPlansId,
                    PreProductionPlanId = plan.PreProductionPlanId,
                    PPDocumentNumber = plan.PPDocumentNumber,
                    SaleContractId=plan.SaleContractId,
                    PPlanName = plan.PPlanName,
                    PPTotalQuantity = plan.PPTotalQuantity,
                    Whetherautomatic = plan.Whetherautomatic,
                    PreProductionState = plan.PreProductionState,
                    PPlanSmark = plan.PPlanSmark,
                    AddName= plan.AddName,
                    AddTime = plan.AddTime,
                });  
            if (request.PreProductionState > 0)
            {
                list = list.Where(plan => plan.PreProductionState == request.PreProductionState);
            }
            if (!string.IsNullOrEmpty(request.PPDocumentNumber))
            {
                list = list.Where(plan => plan.PPDocumentNumber.Contains(request.PPDocumentNumber));
            }
            if (!string.IsNullOrEmpty(request.PPlanName))
            {
                list = list.Where(plan => plan.PPlanName.Contains(request.PPlanName));
            }
            // 执行查询并获取总数  
            var totalCount = await list.CountAsync();

            // 应用分页  
            var sourceProductContracts = await list
                .OrderByDescending(plan => plan.PreProductionPlanId)
                .Skip((request.PageIndex - 1) * request.PageSize)
                .Take(request.PageSize)
                .ToListAsync();

            // 准备结果  
            var pages = new PageDTO<SelectProductionPlanDto>
            {
                Totalcount = totalCount,
                Pagecount = (int)Math.Ceiling(totalCount * 1.0 / request.PageSize),
                list = sourceProductContracts
            };

            return pages;
        }
    }
}
